﻿Imports DTO
Imports System.Data.SqlClient

Public Class ChiTietHoaDonDAO

    'Thêm chi tiết hóa đơn
    Public Sub Them(ByVal cthddto As ChiTietHoaDonDTO)


        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        Dim cn As SqlConnection
        cn = DatabaseProvider.ConnectionData()
        'B3: Tao chuoi strSQL thao tac CSDL
        Dim strSQL As String
        strSQL = "Insert into CHITIETHOADON(MaHoaDon,MaSach,SoLuong,DonGiaBan,MaTheLoai) values(@MaHoaDon,@MaSach,@SoLuong,@DonGiaBan,@MaTheLoai)"
        'B4: Thuc thi chuoi strSQL
        Dim cmd As New SqlCommand(strSQL, cn)

        cmd.Parameters.Add("@MaHoaDon", SqlDbType.Int)
        cmd.Parameters("@MaHoaDon").Value = cthddto.MaHoaDon

        cmd.Parameters.Add("@MaSach", SqlDbType.Int)
        cmd.Parameters("@MaSach").Value = cthddto.MaSach

        cmd.Parameters.Add("@SoLuong", SqlDbType.Int)
        cmd.Parameters("@SoLuong").Value = cthddto.SoLuong

        cmd.Parameters.Add("@DonGiaBan", SqlDbType.Int)
        cmd.Parameters("@DonGiaBan").Value = cthddto.DonGiaBan

        cmd.Parameters.Add("@MaTheLoai", SqlDbType.Int)
        cmd.Parameters("@MaTheLoai").Value = cthddto.MaTheLoai

        cmd.ExecuteNonQuery()
        strSQL = "Select @@IDENTITY"
        cmd = New SqlCommand(strSQL, cn)
        cthddto.MaCTHoaDon = cmd.ExecuteScalar()
        'B5: Dong ket noi CSDL
        cn.Close()
    End Sub

End Class
